using Microsoft.EntityFrameworkCore;
using RestfulApi.Models;

namespace RestfulApi.Data
{
    /// <summary>
    /// 应用程序数据库上下文
    /// </summary>
    public class ApplicationDbContext : DbContext
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="options">数据库上下文选项</param>
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        /// <summary>
        /// 图书数据集
        /// </summary>
        public DbSet<Book> Books { get; set; } = null!;

        /// <summary>
        /// 模型创建配置
        /// </summary>
        /// <param name="modelBuilder">模型构建器</param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 配置Book实体
            modelBuilder.Entity<Book>()
                .Property(b => b.Price)
                .HasColumnType("decimal(18, 2)");

            // 添加种子数据
            modelBuilder.Entity<Book>().HasData(
                new Book
                {
                    Id = 1,
                    Title = "C# 高级编程",
                    Author = "张三",
                    PublishYear = 2023,
                    ISBN = "978-1-234567-89-0",
                    Price = 89.90m,
                    IsAvailable = true,
                    Category = "编程",
                    Description = "这是一本关于C#高级编程的书籍，涵盖了最新的C#特性和最佳实践。"
                },
                new Book
                {
                    Id = 2,
                    Title = "ASP.NET Core 实战",
                    Author = "李四",
                    PublishYear = 2022,
                    ISBN = "978-2-345678-90-1",
                    Price = 79.90m,
                    IsAvailable = true,
                    Category = "Web开发",
                    Description = "这本书详细介绍了ASP.NET Core的各种功能和使用方法。"
                },
                new Book
                {
                    Id = 3,
                    Title = "Entity Framework Core 入门与实践",
                    Author = "王五",
                    PublishYear = 2021,
                    ISBN = "978-3-456789-01-2",
                    Price = 69.90m,
                    IsAvailable = false,
                    Category = "数据库",
                    Description = "本书介绍了Entity Framework Core的基本概念和高级用法。"
                }
            );
        }
    }
}